package com.yztc.administrator.meishi.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.viewpagerindicator.TabPageIndicator;
import com.yztc.administrator.meishi.R;
import com.yztc.administrator.meishi.adapter.ViewPagerAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends FragmentActivity {

    @ViewInject(R.id.tab_page)
    private TabPageIndicator tabPageIndicator;

    @ViewInject(R.id.vp)
    private ViewPager viewPager;

    private ViewPagerAdapter adapter;
    private ListView lv_drawable;
    private ViewPager mVp;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //创建导航栏
        initTab();
        //设置侧滑
        initDrawable();
        Toolbar toolBar = (Toolbar) findViewById(R.id.tool_bar);
        final DrawerLayout drl = (DrawerLayout) findViewById(R.id.drawerLayout);
        mVp = (ViewPager) findViewById(R.id.vp);
        //设置抽屉点击事件
        toolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断抽屉是否打开
                if (!drl.isDrawerOpen(Gravity.LEFT)) {
                    drl.openDrawer(Gravity.LEFT);
                } else {
                    drl.closeDrawer(Gravity.LEFT);
                }
            }
        });

    }

    /* *
       * @author: ZTC
       * @Description: 侧滑功能
       * @data:  2016/9/23  9:46
       * @version: V1.0
       */
    private void initDrawable() {
        lv_drawable = (ListView) findViewById(R.id.drawable_lv);
        int[] imageResId = getImageResId();
        String[] textRes = getTextRes();

        // 构建数据源
        List<Map<String, Object>> data = new ArrayList<>();
        for (int i = 0; i < imageResId.length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("text", textRes[i]);
            map.put("image", imageResId[i]);
            data.add(map);
        }
        SimpleAdapter adapter = new SimpleAdapter(this, data,
                R.layout.drawble_item, new String[]{"text", "image"},
                new int[]{R.id.draw_tv, R.id.draw_iv});
        lv_drawable.setAdapter(adapter);
        lv_drawable.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        Toast.makeText(MainActivity.this, "0", Toast.LENGTH_SHORT).show();
                        break;
                    case 1:
                        //点击跳转收藏页面
                       Intent intent = new Intent(MainActivity.this,CollectionActivity.class);
                        startActivity(intent);
                        break;
                    case 2:
                        Toast.makeText(MainActivity.this, "2", Toast.LENGTH_SHORT).show();
                        break;
                    case 3:
                        Toast.makeText(MainActivity.this, "3", Toast.LENGTH_SHORT).show();
                        break;
                    case 4:
                        Toast.makeText(MainActivity.this, "4", Toast.LENGTH_SHORT).show();
                        break;
                    case 5:
                        SharedPreferences video = getSharedPreferences("video", MODE_PRIVATE);
                        SharedPreferences.Editor edit = video.edit();
                        edit.clear();
                        if (edit.commit()){
                            Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                        }else  if (video.getAll().size() == 0){
                            Toast.makeText(MainActivity.this, "没有更多数据", Toast.LENGTH_SHORT).show();
                        }
                        break;
                }
            }
        });
    }

    //创建导航栏
    private void initTab() {
        ViewUtils.inject(this);
        //创建适配器
        adapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        tabPageIndicator.setViewPager(viewPager);
    }

    private int[] getImageResId() {
        return new int[]{R.mipmap.find_01, R.mipmap.find_02, R.mipmap.find_03, R.mipmap.find_04, R.mipmap.find_05, R.mipmap.find_06};
    }

    private String[] getTextRes() {
        return new String[]{"我的帐号", "收藏", "通用", "意见反馈", "检测更新", "清除下载"};
    }

    //登录跳转
    public void login(View view) {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
    }
    //注册跳转
    public void registers(View view) {
        Intent intent = new Intent(this, RegisterActivity.class);
        startActivity(intent);
    }


}
